local assets = {
	Asset("ANIM", "anim/musha_phoenixhat.zip"),
	Asset("ANIM", "anim/musha_phoenixhat_goggles.zip")
}

local function Updata(inst, display)
	if inst.components.musha_equipment:IsBroken() then
		inst.components.armor:SetAbsorption(0)
		return
	end
	inst.components.armor:SetAbsorption(
		(inst.components.musha_equipment.currentlevel < 30 and 64 + inst.components.musha_equipment.currentlevel or 95) * 0.01
	)
	if not inst.EnergyOn then
		inst.components.equippable.walkspeedmult = math.floor(inst.components.musha_equipment.currentlevel / 10) * 0.03 + 1.02
		inst.components.equippable.dapperness = 0
		inst.components.insulator.insulation = TUNING.INSULATION_MED
		if display then
			inst.components.talker:Say(
				string.format(
					STRINGS.MUSHA_HAT_PHOENIX ..
						" (LV%d)\n" ..
							STRINGS.MUSHA_ARMOR .. " (%d)\n" .. STRINGS.MUSHA_ITEM_WARM .. "(MED)\n" .. STRINGS.MUSHA_ITEM_SPEED .. "(%d)",
					inst.components.musha_equipment.currentlevel,
					inst.components.musha_equipment.currentlevel < 30 and 64 + inst.components.musha_equipment.currentlevel or 95,
					(inst.components.equippable.walkspeedmult - 1) * 100
				)
			)
		end
	else
		inst.components.equippable.walkspeedmult = math.floor(inst.components.musha_equipment.currentlevel / 10) * 0.05 + 0.8
		inst.components.equippable.dapperness =
			inst.components.musha_equipment.currentlevel <= 10 and TUNING.DAPPERNESS_MED or TUNING.DAPPERNESS_LARGE
		inst.components.insulator.insulation = TUNING.INSULATION_LARGE
		if display then
			inst.components.talker:Say(
				string.format(
					"[" ..
						STRINGS.MUSHA_HAT_PHOENIX ..
							" (LV%d)\n" ..
								STRINGS.MUSHA_ARMOR ..
									" (%d)\n" ..
										STRINGS.MUSHA_ITEM_WARM ..
											"(Large)\n" ..
												STRINGS.MUSHA_ITEM_REGEN ..
													"(2)\n" .. STRINGS.MUSHA_ITEM_SANITY_REGEN .. "(%s)\n" .. STRINGS.MUSHA_ITEM_SPEED_DOWN .. "(20)",
					inst.components.musha_equipment.currentlevel,
					inst.components.musha_equipment.currentlevel < 30 and 64 + inst.components.musha_equipment.currentlevel or 95,
					inst.components.musha_equipment.currentlevel <= 10 and "MED" or "Large",
					(1 - inst.components.equippable.walkspeedmult) * 100
				)
			)
		end
	end
end

local function heal_owner(inst, owner)
	if (owner.components.health and owner.components.health:IsHurt()) then
		owner.components.health:DoDelta(1, false)
	end
end

local function Energy_Switch(inst)
	local owner = inst.components.inventoryitem.owner
	if not inst.EnergyOn then
		if inst.components.musha_equipment:IsBroken() then
			return
		end
		inst.heal_owner_task = inst:DoPeriodicTask(6, heal_owner, nil, owner)
		owner.AnimState:OverrideSymbol("swap_hat", "musha_phoenixhat_goggles", "swap_hat")
		inst.sleep_attacker = function(attacked, data)
			local attacker = data.attacker
			if data and attacker and attacker.components.sleeper then
				data.attacker.components.sleeper:AddSleepiness(5, 24)
				SpawnPrefab("small_puff").Transform:SetPosition(attacker:GetPosition():Get())
			end
		end
		inst:ListenForEvent("attacked", inst.sleep_attacker, owner)
	else
		if inst.heal_owner_task then
			inst.heal_owner_task:Cancel()
			inst.heal_owner_task = nil
		end
		owner.AnimState:OverrideSymbol("swap_hat", "musha_phoenixhat", "swap_hat")
		inst:ListenForEvent("attacked", inst.sleep_attacker, owner)
	end
	inst.EnergyOn = not inst.EnergyOn
	Updata(inst)
	inst.components.useableitem:StopUsingItem()
end

local function OnEquip(inst, owner)
	Updata(inst)

	owner.AnimState:OverrideSymbol("swap_hat", "musha_phoenixhat", "swap_hat")

	owner.AnimState:Hide("HAIR")
	owner.AnimState:Hide("HAIR_NOHAT")
	owner.AnimState:Show("HAT_HAIR")
	owner.AnimState:Show("HAT")
end

local function OnUnequip(inst, owner)
	if inst.EnergyOn then
		Energy_Switch(inst)
	end

	if inst.health then
		inst.health:Cancel()
		inst.health = nil
	end

	owner.AnimState:Hide("HAT")
	owner.AnimState:Hide("HAT_HAIR")
	owner.AnimState:Show("HAIR_NOHAT")
	owner.AnimState:Show("HAIR")
end

local function fn()
	local inst = CreateEntity()

	inst.entity:AddTransform()
	inst.entity:AddAnimState()
	inst.entity:AddSoundEmitter()
	inst.entity:AddNetwork()

	MakeInventoryPhysics(inst)
	MakeInventoryFloatable(inst, nil, nil)

	inst:AddComponent("talker")
	inst.components.talker.fontsize = 30
	inst.components.talker.font = TALKINGFONT
	inst.components.talker.colour = Vector3(0.9, 1, 0.75, 1)
	inst.components.talker.offset = Vector3(0, 150, 0)
	inst.components.talker.symbol = "swap_object"

	inst.AnimState:SetBank("beehat")
	inst.AnimState:SetBuild("musha_phoenixhat")
	inst.AnimState:PlayAnimation("anim")

	inst:AddTag("musha_items")

	inst.foleysound = "dontstarve/movement/foley/metalarmour"

	inst.entity:SetPristine()

	if TheNet:GetIsClient() then
		return inst
	end

	inst:AddComponent("useableitem")
	inst.components.useableitem:SetOnUseFn(Energy_Switch)

	inst:AddComponent("equippable")
	inst.components.equippable:SetOnEquip(OnEquip)
	inst.components.equippable:SetOnUnequip(OnUnequip)

	inst:AddComponent("musha_equipment")
	inst.components.musha_equipment:SetUpgradable(true)
	inst.components.musha_equipment:InitMushaEquip("hat", 1800, 0)
	inst.components.musha_equipment:SetOnUpdataFn(Updata)
	inst.components.musha_equipment:SetOnBrokenFn(
		function()
			if inst.EnergyOn then
				Energy_Switch(inst)
				inst.EnergyOn = true
			end
			if inst.components.armor.absorb_percent ~= 0 then
				inst.components.musha_equipment:StopConsuming()
				inst.components.armor:SetAbsorption(0)
				inst.components.equippable.walkspeedmult = 1
				inst.components.talker:Say(
					STRINGS.MUSHA_HAT_BROKEN .. "\n" .. STRINGS.MUSHA_ITEM_DUR .. " (0)\n" .. STRINGS.MUSHA_ITEM_SPEED .. "(0)"
				)
			end
			inst.components.musha_equipment:StopConsuming()
		end
	)

	inst.components.inventoryitem.atlasname = "images/inventoryimages/musha_phoenixhat.xml"

	inst:AddComponent("insulator")
	inst.EnergyOn = false
	inst.type = "phoenix"

	--[[ inst.Rebind = function(inst)
		if inst.EnergyOn then
			inst.EnergyOn = false
			Energy_Switch(inst)
		end
	end ]]
	return inst
end

return Prefab("musha_phoenixhat", fn, assets)
